#!/usr/bin/env bash

source ./config/env.sh
clear

mkdir -p ${K8S_CA_DIR}/kubelet

cd ${K8S_CA_DIR}/kubelet

for node_hostname in ${NODE_HOSTNAME}
  do
    echo "=========================================== ${node_hostname} ==========================================="
    # create token
    export BOOTSTRAP_TOKEN=$(${K8S_BIN_DIR}/kubeadm token create \
      --description kubelet-bootstrap-token \
      --groups system:bootstrappers:${node_hostname} \
      --kubeconfig ~/.kube/config)

    # Setting cluster parameters
    ${K8S_BIN_DIR}/kubectl config set-cluster kubernetes \
      --certificate-authority=${K8S_CA_DIR}/admin/ca.pem \
      --embed-certs=true \
      --server=${APISERVER} \
      --kubeconfig=kubelet-bootstrap-${node_hostname}.kubeconfig

    # Set client authentication parameters
    ${K8S_BIN_DIR}/kubectl config set-credentials kubelet-bootstrap \
      --token=${BOOTSTRAP_TOKEN} \
      --kubeconfig=kubelet-bootstrap-${node_hostname}.kubeconfig

    # Setting context parameters
    ${K8S_BIN_DIR}/kubectl config set-context default \
      --cluster=kubernetes \
      --user=kubelet-bootstrap \
      --kubeconfig=kubelet-bootstrap-${node_hostname}.kubeconfig

    # Setting the default context
    ${K8S_BIN_DIR}/kubectl config use-context default --kubeconfig=kubelet-bootstrap-${node_hostname}.kubeconfig
done


for node_hostname in ${NODE_HOSTNAME}
do
  echo "=========================================== ${node_hostname} <1><0><0>==========================================="
  ssh root@${node_hostname} "test -e ${K8S_CA_DIR}/kubelet/ || mkdir -p ${K8S_CA_DIR}/kubelet/"
  /usr/bin/expect <<-EOF
  spawn scp -o "StrictHostKeyChecking no" ${K8S_CA_DIR}/kubelet/kubelet-bootstrap-${node_hostname}.kubeconfig root@${node_hostname}:${K8S_CA_DIR}/kubelet/kubelet-bootstrap.kubeconfig
  expect {
    
    "(yes/no)?"
    {
        send "yes\n"
        expect "*assword:" {send "$password\n"}
    }
    "*assword:"
    {
        send "$password\n"
        }
    }
    expect eof
EOF
done

mkdir -p ${K8S_DATA}/config/kubelet/yaml

cat <<EOF | tee ${K8S_DATA}/config/kubelet/yaml/kubelet-config.yaml.template
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
authentication:
  anonymous:
    enabled: false
  webhook:
    enabled: true
  x509:
    clientCAFile: "${K8S_CA_DIR}/admin/ca.pem"
authorization:
  mode: Webhook
clusterDomain: "${CLUSTER_DNS_DOMAIN}"
clusterDNS:
  - "${CLUSTER_DNS_SVC_IP}"
podCIDR: "${POD_CIDR}"
maxPods: 220
serializeImagePulls: false
hairpinMode: promiscuous-bridge
cgroupDriver: cgroupfs
runtimeRequestTimeout: "15m"
rotateCertificates: true
serverTLSBootstrap: true
readOnlyPort: 10255
port: 10250
address: "##NODE_IP##"
EOF

for node_ip in ${NODE_IP}
do
  echo "=========================================== ${node_ip} ==========================================="
  sed -e "s/##NODE_IP##/${node_ip}/" ${K8S_DATA}/config/kubelet/yaml/kubelet-config.yaml.template > ${K8S_DATA}/config/kubelet/yaml/kubelet-config-${node_ip}.yaml.template
  ssh root@${node_ip} "test -e ${K8S_DATA}/config/kubelet/yaml || mkdir -p ${K8S_DATA}/config/kubelet/yaml"
  scp ${K8S_DATA}/config/kubelet/yaml/kubelet-config-${node_ip}.yaml.template root@${node_ip}:${K8S_DATA}/config/kubelet/yaml/kubelet-config.yaml
done


# kubelet systemd unit
cat > ${K8S_DATA}/config/kubelet/kubelet.service.template <<EOF
[Unit]
Description=Kubernetes Kubelet
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=docker.service
Requires=docker.service

[Service]
WorkingDirectory=${K8S_DATA}/data/kubelet
ExecStart=${K8S_BIN_DIR}/kubelet \\
  --root-dir=${K8S_DATA}/data/kubelet \\
  --bootstrap-kubeconfig=${K8S_CA_DIR}/kubelet/kubelet-bootstrap.kubeconfig \\
  --cert-dir=${K8S_CA_DIR}/kubelet \\
  --kubeconfig=${K8S_CA_DIR}/kubelet/kubelet.kubeconfig \\
  --config=${K8S_DATA}/config/kubelet/yaml/kubelet-config.yaml \\
  --hostname-override=##NODE_NAME## \\
  --pod-infra-container-image=registry.cn-beijing.aliyuncs.com/k8s_images/pause-amd64:3.1 \\
  --allow-privileged=true \\
  --event-qps=0 \\
  --kube-api-qps=1000 \\
  --kube-api-burst=2000 \\
  --registry-qps=0 \\
  --image-pull-progress-deadline=30m \\
  --logtostderr=true \\
  --read-only-prot    \\
  --v=2
Restart=always
RestartSec=5
StartLimitInterval=0

[Install]
WantedBy=multi-user.target
EOF


for node_hostname in ${NODE_HOSTNAME}
do
  echo "=========================================== ${node_hostname} <1><0><1>==========================================="
  sed -e "s/##NODE_NAME##/${node_hostname}/" ${K8S_DATA}/config/kubelet/kubelet.service.template > ${K8S_DATA}/config/kubelet/kubelet-${node_hostname}.service
  scp -o "StrictHostKeyChecking no" ${K8S_DATA}/config/kubelet/kubelet-${node_hostname}.service root@${node_hostname}:/etc/systemd/system/kubelet.service
done

${K8S_BIN_DIR}/kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --group=system:bootstrappers


for node_ip in ${NODE_IP}
do
  echo "=========================================== ${node_ip} ==========================================="
  ssh root@${node_ip} "/usr/sbin/swapoff -a"
  ssh root@${node_ip} "test -e ${K8S_DATA}/data/kubelet || mkdir -p ${K8S_DATA}/data/kubelet"
  ssh root@${node_ip} "systemctl daemon-reload && systemctl enable kubelet && systemctl start kubelet"
done


for node_ip in ${NODE_IP}
do
  echo "=========================================== ${node_ip} ==========================================="
  ssh root@${node_ip} "systemctl status kubelet | grep Active"
done
